package com.mlizhi.widgets.chart;

import com.mlizhi.kingdom.R;
import com.mlizhi.base.Utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class ChartView4SeekBar extends View {

	private TypedArray array4SeekBar = null;
	private float seekBar4barPos = 0.0f;
	private Context context = null;
	public ChartView4SeekBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.array4SeekBar = context.obtainStyledAttributes(attrs,R.styleable.ChartView4SeekBar);
		this.context = context;
	}

	public ChartView4SeekBar(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int axis4x = this.getMeasuredWidth();
		int axis4y = this.getMeasuredHeight();
		Paint paint = new Paint();
//		String seekBar4text = this.array4SeekBar.getString(R.styleable.ChartView4SeekBar_seekBar4text);
//		String seekBar4min = this.array4SeekBar.getString(R.styleable.ChartView4SeekBar_seekBar4min);
//		String seekBar4max = this.array4SeekBar.getString(R.styleable.ChartView4SeekBar_seekBar4max);
//		String seekBar4min = "小";
//		String seekBar4max = "大";
        int seekBar4type = this.array4SeekBar.getInt(R.styleable.ChartView4SeekBar_seekBar4type,0);
//        seekBar4barPos = this.array4SeekBar.getFloat(R.styleable.ChartView4SeekBar_seekBar4barPos, 0.32f);
        int seekBar4thumbColor = this.array4SeekBar.getColor(R.styleable.ChartView4SeekBar_seekBar4thumbColor, Color.WHITE);
        float seekBar4strokeWidth = this.array4SeekBar.getDimension(R.styleable.ChartView4SeekBar_seekBar4strokeWidth, 3.0f);
        boolean seekBar4antiAlias = this.array4SeekBar.getBoolean(R.styleable.ChartView4SeekBar_seekBar4antiAlias, true);
        
        paint.setAntiAlias(seekBar4antiAlias);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(seekBar4strokeWidth);
        paint.setColor(seekBar4thumbColor);
        paint.setTextSize(Utils.dip2px(context, 12.0f));
        
//        canvas.drawText(seekBar4text, axis4x*0.05f, axis4y*0.5f, paint);
        Bitmap bitmap4rect = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_seekbar4rect_bg);
        Bitmap bitmap4thr = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_seekbar4thr_bg);
        Bitmap bitmap4arrow = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_arrow_pos);
        /**
         * 画pos的位置公式：(0.96-0.06)*axis4x/(0.6-0.2)*(pos-0.2)+0.06*axis4x=posX
         * 化简得：(2.25f*pos-0.39f)*axis4x=posX
         * 针对三角形的，计算pos图片倾斜度(0.9-0.3)*axis4y/[(0.96-0.06)*axis4x]*[(pos-0.2)*(0.96-0.06)*axis4x/(0.6-0.2)]=offsetY
         * 化简得：offsetY=1.5*(pos-0.2)*axis4y
         * 针对圆角矩形的rate公式：rate = 100/(0.6-0.2)*(pos-0.2)
         */
        if(seekBar4type == 0){
        	canvas.drawBitmap(bitmap4rect, null, new RectF(axis4x*0.06f, axis4y*0.5f, axis4x*0.96f, axis4y*0.9f), paint);
        	canvas.drawBitmap(bitmap4arrow, null, new RectF((2.25f*seekBar4barPos-0.39f)*axis4x-10.0f, axis4y*0.35f, (2.25f*seekBar4barPos-0.39f)*axis4x+10.0f, axis4y*0.6f), paint);
//        	String rate = String.valueOf(new BigDecimal(250f*seekBar4barPos-50f).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        	String rate = String.valueOf((int) ((2.50f*seekBar4barPos-0.50f)*100));
        	canvas.drawText(rate, (2.25f*seekBar4barPos-0.39f)*axis4x-0.5f*paint.measureText(rate), axis4y*0.25f, paint);
        }else if(seekBar4type == 1){
        	paint.setTextSize(Utils.dip2px(context, 18.0f));
        	canvas.drawBitmap(bitmap4thr, null, new RectF(axis4x*0.06f, axis4y*0.3f, axis4x*0.96f, axis4y*0.9f), paint);
        	float offsetY = 1.5f*(seekBar4barPos-0.2f)*axis4y;
        	canvas.drawBitmap(bitmap4arrow, null, new RectF((2.25f*seekBar4barPos-0.39f)*axis4x-10.0f, axis4y*0.5f-offsetY, (2.25f*seekBar4barPos-0.39f)*axis4x+10.0f, axis4y*0.9f-offsetY+5f), paint);
        	canvas.drawText(context.getResources().getString(R.string.chart_view_small), axis4x*0.060f, axis4y*0.75f, paint);
        	canvas.drawText(context.getResources().getString(R.string.chart_view_big), axis4x*0.86f, axis4y*0.75f, paint);
        }
        
        
        
	}

	public float getSeekBar4barPos() {
		return seekBar4barPos;
	}

	public void setSeekBar4barPos(float seekBar4barPos) {
		this.seekBar4barPos = seekBar4barPos;
	}
	
	
	
}
